package com.gitee.kamismile.gatewayAgent.client.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.timeout.IdleStateHandler;
import com.gitee.kamismile.gatewayAgent.protobuf.Message;

import java.util.concurrent.TimeUnit;

/**
 * Created by lidong on 2017/2/8.
 */
public class GateWayClientInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline p = ch.pipeline();
//        if (sslCtx != null) {
//            p.addLast(sslCtx.newHandler(ch.alloc(), GateWayClient.HOST, GateWayClient.PORT));
//        }

        p.addLast(new ProtobufVarint32FrameDecoder());
        p.addLast(new ProtobufDecoder(Message.RouterMessage.getDefaultInstance()));

        p.addLast(new ProtobufVarint32LengthFieldPrepender());
        p.addLast(new ProtobufEncoder());

        //顺序问题 不然 定时无效
        p.addLast(new IdleStateHandler(0, 0, 88, TimeUnit.SECONDS));
        p.addLast(new ClientHeartBeatHandler());

        p.addLast(new GateWayClientHandler());


    }

}
